# -*- codeing = utf-8 -*-

from django.shortcuts import render

from chit import models
from utils.pagination import Pagination
from utils.checkfilter import CheckFilter
from utils.selectfilter import SelectFilter


def caselist(request):
    # 根据url做筛选，筛选条件（根据用户通过GET传过来的参数实现）
    # ?type=0&type=1
    allow_filter_name = ['type', 'result','poeple_result']
    condition = {}
    for name in allow_filter_name:
        value_list = request.GET.getlist(name)  # [1,2]
        print(value_list)
        if not value_list:
            continue
        # print('{}__in'.format(name))      # 双下划线查询
        condition['{}__in'.format(name)] = value_list
        """
            condition = {
                "type__in":[1,2],
            }
        """

    all_obj = models.ChitCaseList.objects.all().filter(**condition)
    page_object = Pagination(
        current_page=request.GET.get('page'),
        all_count=all_obj.count(),
        base_url=request.path_info,
        query_params=request.GET,
        per_page=15
    )
    issues_object_list = all_obj[page_object.start:page_object.end]

    auto_result_objects = models.ChitCaseList.objects.all().values_list('result','result')
    people_result = models.ChitCaseList.objects.all().values_list('poeple_result', 'poeple_result')
    # 去重
    auto_result_objects = tuple(set(list(auto_result_objects)))
    people_result = tuple(set(list(people_result)))

    content = {
        'objs': all_obj,
        'issues_object_list': issues_object_list,
        'page_html': page_object.page_html(),
        'type_filter': CheckFilter('type', models.ChitCaseList.type_choice, request),
        'auto_result_filter': SelectFilter('result', auto_result_objects, request),
        'people_result_filter': SelectFilter('poeple_result', people_result, request),
    }

    return render(request, 'chit/chitbasecase.html', content)


